﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OD.ProjectCustomizationTool.Helpers
{
    internal static class SolutionHelper
    {
        /// <summary>
        /// Search for the first solution under a path.
        /// </summary>
        /// <param name="path">The path to consider.</param>
        /// <returns>Returns the full path of the solution.</returns>
        public static string GetFullPath(String path)
        {
            if (String.IsNullOrEmpty(path))
            {   // This code has been generated from the 'argnullString' custom snippet.
                throw new ArgumentNullException("path");
            }
            if (String.IsNullOrEmpty(path.Trim()))
            {   // This code has been generated from the 'argnullString' custom snippet.
                throw new ArgumentException("The argument 'path' contains only white spaces.", "path");
            }
            
            if (File.Exists(path))
            {   // If the path is a file, continue to search on the folder.
                return SolutionHelper.GetFullPath(Directory.GetParent(path).FullName);
            }
            else if (Directory.GetFiles(path, Properties.Resources.SolutionFileSearchPattern).Count() > 0)
            {   // If a solution exists here, return that path.
                return path;
            }
            else if (Directory.GetParent(path) == null)
            {
                throw new InvalidOperationException("No solution file has been found.");
            }
            else
            {   // Otherwise, continue with the parent folder.
                return SolutionHelper.GetFullPath(Directory.GetParent(path).FullName);
            }
        }
    }
}
